package com.example.dashuju.service.impl;

import com.example.dashuju.repository.SalesRepository;
import com.example.dashuju.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SalesServiceImpl implements SalesService {

    @Autowired
    private SalesRepository salesRepository;

    @Override
    public Map<String, Object> getCategorySales() {
        List<Object[]> results = salesRepository.findCategorySales();

        List<String> categories = new ArrayList<>();
        List<Double> sales = new ArrayList<>();

        for (Object[] result : results) {
            categories.add((String) result[0]);
            sales.add((Double) result[1]);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("categories", categories);
        data.put("sales", sales);
        return data;
    }

    @Override
    public Map<String, Object> getRegionSales() {
        List<Object[]> results = salesRepository.findRegionSales();

        List<String> regions = new ArrayList<>();
        List<Double> sales = new ArrayList<>();

        for (Object[] result : results) {
            regions.add((String) result[0]);
            sales.add((Double) result[1]);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("regions", regions);
        data.put("sales", sales);
        return data;
    }

    @Override
    public Map<String, Object> getPlatformTrend() {
        List<Object[]> results = salesRepository.findPlatformTrend();

        // 初始化24小时数据
        List<String> times = new ArrayList<>();
        for (int i = 1; i <= 24; i++) {
            times.add(String.format("%02d", i));
        }

        Map<String, List<Double>> platformData = new HashMap<>();
        platformData.put("安卓", new ArrayList<>(Collections.nCopies(24, 0.0)));
        platformData.put("IOS", new ArrayList<>(Collections.nCopies(24, 0.0)));

        for (Object[] result : results) {
            String platform = (String) result[0];
            int hour = (Integer) result[1];
            Double amount = (Double) result[2];

            if (platformData.containsKey(platform) && hour >= 1 && hour <= 24) {
                platformData.get(platform).set(hour - 1, amount);
            }
        }

        Map<String, Object> data = new HashMap<>();
        data.put("times", times);
        data.put("android", platformData.get("安卓"));
        data.put("ios", platformData.get("IOS"));
        return data;
    }

    @Override
    public Map<String, Object> getKeyRegionsSales() {
        List<Object[]> results = salesRepository.findKeyRegionsSales();

        List<String> regions = new ArrayList<>();
        List<Double> sales = new ArrayList<>();

        for (Object[] result : results) {
            regions.add((String) result[0]);
            sales.add((Double) result[1]);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("regions", regions);
        data.put("sales", sales);
        return data;
    }

    @Override
    public Map<String, Object> getCoreRegionsRatio() {
        List<Object[]> results = salesRepository.findCoreRegionsRatio();

        double total = results.stream()
                .mapToDouble(result -> (Double) result[1])
                .sum();

        List<Map<String, Object>> ratioData = new ArrayList<>();
        for (Object[] result : results) {
            String region = (String) result[0];
            Double amount = (Double) result[1];
            double percentage = (amount / total) * 100;

            Map<String, Object> regionData = new HashMap<>();
            regionData.put("name", region);
            regionData.put("value", Math.round(percentage));
            ratioData.add(regionData);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("data", ratioData);
        return data;
    }

    @Override
    public Map<String, Object> getAgeDistribution() {
        List<Object[]> results = salesRepository.findAgeDistribution();

        List<Map<String, Object>> ageData = results.stream()
                .map(result -> {
                    Map<String, Object> item = new HashMap<>();
                    item.put("name", result[0]);
                    item.put("value", result[1]);
                    return item;
                })
                .collect(Collectors.toList());

        Map<String, Object> data = new HashMap<>();
        data.put("data", ageData);
        return data;
    }

    @Override
    public Map<String, Object> getOccupationDistribution() {
        List<Object[]> results = salesRepository.findOccupationDistribution();

        List<Map<String, Object>> occupationData = results.stream()
                .map(result -> {
                    Map<String, Object> item = new HashMap<>();
                    item.put("name", result[0]);
                    item.put("value", result[1]);
                    return item;
                })
                .collect(Collectors.toList());

        Map<String, Object> data = new HashMap<>();
        data.put("data", occupationData);
        return data;
    }

    @Override
    public Map<String, Object> getInterestDistribution() {
        List<Object[]> results = salesRepository.findInterestDistribution();

        List<Map<String, Object>> interestData = results.stream()
                .map(result -> {
                    Map<String, Object> item = new HashMap<>();
                    item.put("name", result[0]);
                    item.put("value", result[1]);
                    return item;
                })
                .collect(Collectors.toList());

        Map<String, Object> data = new HashMap<>();
        data.put("data", interestData);
        return data;
    }

    @Override
    public Map<String, Object> getYearlySales() {
        List<Object[]> results = salesRepository.findYearlySales();

        Map<String, Object> data = new HashMap<>();
        for (Object[] result : results) {
            Integer year = (Integer) result[0];
            Double amount = (Double) result[1];
            data.put("sales" + year, amount);
        }

        return data;
    }
}